package com.qianxing.exceptionhandler;

import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * Author:JasonLong
 * Date:2023/5/11
 * Time:17:03
 * 异常响应类
 */
public class ResponseException {
    /**
     * 响应代码
     */
    private String errCode;

    /**
     * 响应消息
     */
    private String errMessage;

    /**
     * 响应数据
     */
    private Object data;

    //无参构造器
    public ResponseException() {
    }

    public String getErrCode() {
        return errCode;
    }

    public void setErrCode(String code) {
        this.errCode = code;
    }

    public String getErrMessage() {
        return errMessage;
    }

    public void setErrMessage(String message) {
        this.errMessage = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 成功
     */
    public static ResponseException success() {
        ResponseException rb = new ResponseException();
        rb.setErrCode(EnumException.SUCCESS.getResultCode());
        rb.setErrMessage(EnumException.SUCCESS.getResultMsg());
        return rb;
    }

    public static ResponseException success(String code, String message) {
        ResponseException rb = new ResponseException();
        rb.setErrCode(code);
        rb.setErrMessage(message);
        return rb;
    }

    /**
     * 返回List数据
     *
     * @param data
     * @return
     */
    public static ResponseException success(List<?> data) {
        ResponseException rb = new ResponseException();
        rb.setErrCode(EnumException.SUCCESS.getResultCode());
        rb.setErrMessage(EnumException.SUCCESS.getResultMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 返回任意对象
     * @param data
     * @return
     */
    public static ResponseException success(Object data) {
        ResponseException rb = new ResponseException();
        rb.setErrCode(EnumException.SUCCESS.getResultCode());
        rb.setErrMessage(EnumException.SUCCESS.getResultMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 失败
     * 在BaseAbnormalResponseInterface被实现的情况下可调用该方法,接口作用于易于拓展
     */
    public static ResponseException error(BaseAbnormalResponseInterface baseAbnormalResponseInterface) {
        ResponseException rb = new ResponseException();
        rb.setErrCode(baseAbnormalResponseInterface.getResultCode());
        rb.setErrMessage(baseAbnormalResponseInterface.getResultMsg());
        return rb;
    }

    /**
     * 失败
     * 使用自定义Code和Msg时
     */
    public static ResponseException error(String code, String message) {
        ResponseException rb = new ResponseException();
        rb.setErrCode(code);
        rb.setErrMessage(message);
        return rb;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
